package com.task.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonRootName;
import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonRootName(value = "result")
@Data
public class Result<T> implements Serializable {

    /**
     * Default serialVersionUID
     */
    private static final long serialVersionUID = 1L;

    /**
     * 默认状态码
     */
    private static final String STATU_OK_CODE = "0";

    /**
     * 成功状态码
     */
    public static final String OK = STATU_OK_CODE;

    /**
     * 本次操作状态(-1/0/其他),默认值为{code:0,msg:""} <br>
     * 0：正常<br>
     * -1：失败<br>
     * 其他：自定义值
     */
    @JsonProperty("code")
    private String code = STATU_OK_CODE;

    /**
     * 消息
     */
    @JsonProperty("msg")
    private String msg = "";

    /**
     * 数据对象
     */
    @JsonProperty("data")
    private T data;

    /**
     * 系统当前时间戳
     */
    @JsonProperty("times")
    private Long times = System.currentTimeMillis();


    public Result() {

    }

    public Result(T data) {
        this.data = data;
    }

    public Result(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Result(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }


    /**
     * 返回错误信息，指定code和msg<br>
     *
     * @param code 状态码
     * @param msg  消息内容
     * @return Result
     */
    public static <T> Result<T> error(String code, String msg) {
        return new Result<T>(code, msg);
    }


    public static <T> Result<T> error(String code, String msg, T data) {
        return new Result<T>(code, msg, data);
    }


    /**
     * 返回默认成功信息 code:0 msg:""
     *
     * @return Result
     */
    public static Result ok() {
        return new Result();
    }

    /**
     * 返回数据
     *
     * @param data 要返回的数据
     * @return Result
     */
    public static <T> Result<T> ok(T data) {
        return new Result(data);
    }

    /**
     * 返回默认成功信息 code:0 msg:""
     *
     * @param k data对象节点中的key
     * @param v value
     * @return
     */
    public static Result<Map<String, Object>> ok(String k, Object v) {
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put(k, v);
        return new Result(resultMap);
    }
}

